using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
namespace Microsoft.Phone.Controls.Maps.Core
{
	internal abstract class MapConfigurationProvider
	{
		private const string RootNodeName = "configuration";
		private const string versionAttribute = "version";
		private const string cultureAttribute = "culture";
		private const string PropertyNodeName = "add";
		private const string KeyAttribute = "key";
		private const string ValueAttribute = "value";
		public abstract event EventHandler<MapConfigurationLoadedEventArgs> Loaded;
		protected Dictionary<string, MapConfigurationSection> Sections
		{
			get;
			set;
		}
		public abstract void LoadConfiguration();
		public abstract void GetConfigurationSection(string version, string sectionName, string culture, MapConfigurationCallback callback, object userState);
		protected static string GetConfigurationKey(string version, string sectionName, string culture)
		{
			if (string.IsNullOrEmpty(culture))
			{
				culture = string.Empty;
			}
			return string.Format(CultureInfo.get_InvariantCulture(), "{0}_{1}_{2}", new object[]
			{
				version,
				sectionName,
				culture
			}).ToUpper(CultureInfo.get_InvariantCulture());
		}
		protected MapConfigurationSection GetSection(string version, string sectionName, string culture)
		{
			MapConfigurationSection result = null;
			if (this.Sections != null)
			{
				string configurationKey = MapConfigurationProvider.GetConfigurationKey(version, sectionName, culture);
				if (this.Sections.ContainsKey(configurationKey))
				{
					result = this.Sections.get_Item(configurationKey);
				}
				else
				{
					if (!string.IsNullOrEmpty(culture))
					{
						configurationKey = MapConfigurationProvider.GetConfigurationKey(version, sectionName, string.Empty);
						if (this.Sections.ContainsKey(configurationKey))
						{
							result = this.Sections.get_Item(configurationKey);
						}
					}
				}
			}
			return result;
		}
		protected bool ContainConfigurationSection(string version, string sectionName, string culture)
		{
			return this.Sections != null && this.Sections.ContainsKey(MapConfigurationProvider.GetConfigurationKey(version, sectionName, culture));
		}
		protected static Dictionary<string, MapConfigurationSection> ParseConfiguration(XmlReader sectionReader)
		{
			if (sectionReader == null)
			{
				throw new ConfigurationNotLoadedException(ExceptionStrings.ConfigurationException_NullXml);
			}
			Dictionary<string, MapConfigurationSection> dictionary = new Dictionary<string, MapConfigurationSection>();
			if (sectionReader.Read() && sectionReader.IsStartElement() && sectionReader.get_LocalName() == "configuration")
			{
				while (sectionReader.Read())
				{
					if (sectionReader.IsStartElement() && sectionReader.get_LocalName() != "configuration")
					{
						string text = sectionReader.get_Item("version");
						string localName = sectionReader.get_LocalName();
						string text2 = sectionReader.get_Item("culture");
						if (string.IsNullOrEmpty(localName) || string.IsNullOrEmpty(text))
						{
							throw new XmlException(ExceptionStrings.MapConfiguration_ParseConfiguration_InvalidSection_NoVersion);
						}
						string configurationKey = MapConfigurationProvider.GetConfigurationKey(text, localName, text2);
						if (dictionary.ContainsKey(configurationKey))
						{
							throw new XmlException(string.Format(CultureInfo.get_InvariantCulture(), ExceptionStrings.MapConfiguration_ParseConfiguration_DuplicateSection, new object[]
							{
								localName,
								text,
								text2
							}));
						}
						dictionary.Add(configurationKey, MapConfigurationProvider.ParseConfigurationSection(sectionReader.ReadSubtree()));
					}
				}
				return dictionary;
			}
			throw new XmlException(string.Format(CultureInfo.get_InvariantCulture(), ExceptionStrings.MapConfiguration_ParseConfiguration_InvalidRoot, new object[]
			{
				sectionReader.get_LocalName()
			}));
		}
		private static MapConfigurationSection ParseConfigurationSection(XmlReader sectionReader)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			if (sectionReader.Read() && !sectionReader.get_IsEmptyElement())
			{
				while (sectionReader.Read())
				{
					if (sectionReader.IsStartElement())
					{
						if (!(sectionReader.get_LocalName() == "add"))
						{
							throw new XmlException(string.Format(CultureInfo.get_InvariantCulture(), ExceptionStrings.MapConfiguration_ParseConfiguration_InvalidTag, new object[]
							{
								sectionReader.get_LocalName()
							}));
						}
						string text = sectionReader.get_Item("key").ToUpper(CultureInfo.get_InvariantCulture());
						string text2 = sectionReader.get_Item("value");
						if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2))
						{
							if (dictionary.ContainsKey(text))
							{
								throw new XmlException(string.Format(CultureInfo.get_InvariantCulture(), ExceptionStrings.MapConfiguration_ParseConfiguration_DuplicateNodeKey, new object[]
								{
									text
								}));
							}
							dictionary.Add(text, text2);
						}
					}
				}
			}
			return new MapConfigurationSection(dictionary);
		}
	}
}
